﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Weibo2Sfdc
{
    public partial class UserConfigForm : Form
    {

        private WebBrowser web_main;

        public UserConfigForm()
        {
            InitializeComponent();
        }

        // 重载构造方法,获取 web_main instance
        public UserConfigForm(WebBrowser param_web_main)
        {
            InitializeComponent();
            this.web_main = param_web_main;
        }

        private void UserConfig_Load(object sender, EventArgs e)
        {
            this.WeiboUsername.Text = Properties.Settings.Default.WeiboUsername;
            this.WeiboPassword.Text = Properties.Settings.Default.WeiboPassword;
            this.SfdcUsername.Text = Properties.Settings.Default.SfdcUsername;
            this.SfdcPassword.Text = Properties.Settings.Default.SfdcPassword;

        }

        private void WBUserConfig_Click(object sender, EventArgs e)
        {
            
        }

        private void WeiboInfoSave_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.WeiboUsername = this.WeiboUsername.Text;
            Properties.Settings.Default.WeiboPassword = this.WeiboPassword.Text;

            DialogResult res = MessageBox.Show( 
                    "你确定修改吗？\n修改之后，微博将自动退出。", "提示",
                    MessageBoxButtons.YesNo,  MessageBoxIcon.Question       );
            if( res == DialogResult.Yes )
            {
                 Properties.Settings.Default.Save();
                 MessageBox.Show("修改成功！");
                 if( web_main != null ){
                     web_main.Navigate("http://weibo.com/logout.php?backurl=http%3A%2F%2Fs.weibo.com");
                 }

            }
           
        }

       

        public void SelectTab( String  tabName) {
            this.ConfigTabControl.SelectTab(tabName);
        }

        /**
         * 保存配置信息
         */
        private void ConfigFinish_Click(object sender, EventArgs e)
        {
            
            if( String.IsNullOrEmpty(this.WeiboUsername.Text) || String.IsNullOrEmpty(this.WeiboPassword.Text) )
            {
                MessageBox.Show("微博 账号和密码不能为空！\n请重新检查。");
                this.SelectTab("WeiboUserConfig"); 
            }
            else if (String.IsNullOrEmpty(this.SfdcUsername.Text) || String.IsNullOrEmpty(this.SfdcPassword.Text))
            {
                MessageBox.Show("Salesforce 账号和密码不能为空！\n请重新检查。");
                this.SelectTab("SfdcUserConfig");
            }
            else
            {
                Properties.Settings.Default.SfdcUsername = this.SfdcUsername.Text;
                Properties.Settings.Default.SfdcPassword = this.SfdcPassword.Text;
                Properties.Settings.Default.WeiboUsername = this.WeiboUsername.Text;
                Properties.Settings.Default.WeiboPassword = this.WeiboPassword.Text;

                Properties.Settings.Default.Save();
                this.Close();
            
            }

        }

        private void ConfigNext_Click(object sender, EventArgs e)
        {
            this.SelectTab("SfdcUserConfig");
        }

        private void ConfigPrev_Click(object sender, EventArgs e)
        {
            this.SelectTab("WeiboUserConfig");
        }

       

    }
}
